﻿
using MES.Server.Common;
using MES.Server.Entities;
using MES.Server.IService;
using MES.Server.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MES.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    
    public class UserController : ControllerBase
    {
       
        IUserService _userService;
        public UserController(IUserService userService)
        {
            
            _userService = userService;
        }
        [HttpPost("login")]
        public async Task<JsonResult> Login([FromForm] string un, [FromForm] string pw)
        {
           
            // 结果封装
            // {
            //    "state":0
            //    "message":"异常消息"
            //    "data":{
            //        "EId":345345,
            //        "Name":"sefsegseg"
            //    }
            // }
            ApiDataResult<UserEntity> result = new ApiDataResult<UserEntity>();
            try
            {
                //throw new NotImplementedException();
                var data = _userService.CheckLogin(un, pw);
                if (data == null)
                {
                    result.Success = false;
                    result.OValue = 404;
                    result.Message = "用户名或密码错误";
                }
                else
                    result.Data = data;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }


        [HttpGet("logintest")]
        public ActionResult LoginTest(string un, string pw)
        {
            return Ok("Login test");
        }

        [HttpGet("test")]
        [Authorize]
        public ActionResult Test()
        {
            return Ok("Zhaoxi");
        }

        [HttpPost("update_pwd")]
        [Authorize]
        public async Task<JsonResult> UpdatePassword([FromForm] int id, [FromForm] string opd, [FromForm] string npd)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                _userService.UpdatePassword(id, opd, npd);
                result.Data = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }

            return await Task.FromResult(new JsonResult(result));
        }

        //[HttpPost("list")]
        //[Authorize]
        //public async Task<JsonResult> GetUsers(UserConditionEntity condition)
        //{
        //    ApiDataResult<UserEntity[]> result = new ApiDataResult<UserEntity[]>();
        //    try
        //    {
        //        condition.SearchKey =
        //            (condition.SearchKey == "none" ? "" : condition.SearchKey);

        //        result.Data = _userService.GetUsers(
        //            condition.SearchKey,
        //            condition.DepartIds,
        //            condition.PostIds);
        //    }
        //    catch (Exception ex)
        //    {
        //        result.Success = false;
        //        result.OValue = 500;
        //        result.Message = ex.Message;
        //    }

        //    return await Task.FromResult(new JsonResult(result));
        //}

        [HttpPost("list/{index}/{size}")]
        [Authorize]
        public async Task<JsonResult> GetUsers(UserConditionEntity condition, [FromRoute] int index, [FromRoute] int size)
        {
            ApiDataResult<Page<UserEntity[]>> result = new ApiDataResult<Page<UserEntity[]>>();
            try
            {
                int total = 0;

                condition.SearchKey =
                    (condition.SearchKey == "none" ? "" : condition.SearchKey);

                var users = _userService.GetUsers(
                    condition.SearchKey,
                    condition.DepartIds,
                    condition.PostIds,
                    index,
                    size,
                    ref total).ToArray();

                // 添加一层分页信息
                Page<UserEntity[]> page = new Page<UserEntity[]>();
                page.PageIndex = index;
                page.PageSize = size;
                page.TotalCount = total;
                page.Data = users;

                result.Data = page;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("ids")]
        [Authorize]
        public async Task<JsonResult> GetUsersByIds(int[] ids)
        {
            ApiDataResult<UserEntity[]> result = new ApiDataResult<UserEntity[]>();
            try
            {
                result.Data = _userService.GetUsersByIds(ids);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }

            return await Task.FromResult(new JsonResult(result));
        }


        [HttpPost("update")]
        [Authorize]
        public async Task<JsonResult> UpdateUser(UserEntity entity)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _userService.Update(entity);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }


        [HttpGet("delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeleteUser(int[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _userService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        // api/user/lock/2024001/0
        [HttpGet("lock/{id}/{status}")]
        //[Authorize]
        public async Task<JsonResult> LockUser(int id, int status)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _userService.LockUser(id, status);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("check/{id}/{name}")]
        [Authorize]
        public async Task<JsonResult> CheckUserName(string name, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _userService.CheckUserName(name, id);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("save_roles")]
        [Authorize]
        public async Task<JsonResult> SaveUserRoles(RoleUserEntity[] roleUsers)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _userService.SaveUserRoles(roleUsers);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("reset_pwd/{id}")]
        [Authorize]
        public async Task<JsonResult> ResetPassword(int id)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _userService.ResetPassword(id);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
    }
}
